﻿using System.Runtime.CompilerServices;

namespace KinonekoSoftware.CodariaDB.Runtimes.Categories
{
    
    [DebuggerDisplay("{Name} - {GetHashCode()}")]
    public class SystemCatalog : Catalog, INotion, IGlossary
    {
        private string _intro;
        
        public  bool   HasChildren() => Children.Count > 0;
        
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool IsRoot() => string.IsNullOrEmpty(ParentID);

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool HasParent() => !string.IsNullOrEmpty(ParentID);

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public string GetID() => Id;

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public string GetParentID() => ParentID;

        public void AppendChild(SystemCatalog instance)
        {
            Children.Add(instance);
        }
        
        public string       ParentID      { get; set; }
        public int          Index         { get; set; }
        public InstanceType Type          { get; set; }
        public string       CustomType    { get; set; }
        public bool         UseCustomType { get; set; }
        
        [BsonIgnore]
        [JsonIgnore]
        private ObservableList<SystemCatalog> _children;

        [BsonIgnore]
        [JsonIgnore]
        public ObservableList<SystemCatalog> Children => _children ??= new ObservableList<SystemCatalog>();

        /// <summary>
        /// 获取或设置 <see cref="Intro"/> 属性。
        /// </summary>
        public string Intro
        {
            get => _intro;
            set => SetValue(ref _intro, value);
        }

        public int Count => Children.Count;

    }
    
    [Obsolete("use TreeCatalog instead")]
    [DebuggerDisplay("{Name} - {GetHashCode()}")]
    public class FunctionCatalog : Catalog, IHierarchicalObject
    {
        public static readonly FunctionCatalog Empty = new FunctionCatalog();
        private                bool            _isVisible;

        public bool IsVisible
        {
            get => _isVisible;
            set => SetValue(ref _isVisible, value);
        }
        public string      ParentID    { get; set; }
        public CatalogList CatalogType { get; set; }
        public int         Index       { get; set; }

        [BsonIgnore]
        [JsonIgnore]
        public bool IsFunctionNode => CatalogType >= 0;

        [JsonIgnore]
        [BsonIgnore]
        public ObservableCollection<FunctionCatalog> Children { get; set; }



        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool IsRoot() => string.IsNullOrEmpty(ParentID);

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool HasParent() => !string.IsNullOrEmpty(ParentID);

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public string GetID() => Id;

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public string GetParentID() => ParentID;

        public void AppendChild(IHierarchicalObject instance)
        {
            var instance2 = (FunctionCatalog)instance;
            Children ??= new ObservableCollection<FunctionCatalog>();

            Children.Add(instance2);
        }
    }
}